package temp_inventory.remote
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	
	import temp_inventory.bean.TempInventory;

	public class TempInventoryDao extends BaseDao
	{
		private var _service:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:TempInventoryDao;
		
		/**
		 * Singleton
		 */
		public static function getInstance() : TempInventoryDao {
			if ( _instance == null ){
				_instance = new TempInventoryDao();
			}
			return _instance;
		}
		
		function TempInventoryDao(){
			_service.channelSet = this.buildChannelSet();
			_service.destination = "tempInventoryService";
			_service.concurrency = "last";
			_service.makeObjectsBindable = true;
			_service.showBusyCursor = true;
		}
		
		public function save(ti:TempInventory):void{
			_service.addEventListener(ResultEvent.RESULT, processSave);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.saveTempInventory(ti);
		}
		
		private function processSave(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processSave);
			Env.dispatchEvent(new CustomerEvent("processSaveCallBack",e.result as Boolean));
		}
		
		
		public function getTempInventoryList(name:String,colorCode:String, sampleFlag:Boolean, operator:String, startDate:Date, endDate:Date, page:Page):void{
			_service.addEventListener(ResultEvent.RESULT, processGetTempInventoryList);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.getTempInventoryList(name,colorCode,sampleFlag,operator,startDate,endDate,page);
		}
		
		private function processGetTempInventoryList(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,processGetTempInventoryList);
			if(e.result != null){
				var page:Page = e.result as Page;
				Env.dispatchEvent(new CustomerEvent("getTempInventoryListSuccess",page));
			}
		}
		
		public function getTempInventoryDetail(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT, processGetTempInventoryDetail);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.getInventoryDetail(id);
		}
		
		private function processGetTempInventoryDetail(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processGetTempInventoryDetail);
			if(e.result != null){
				Env.dispatchEvent(new CustomerEvent("getTempInventoryDetailSuccess",e.result));
			}
		}
		
		public function updateDetail(name:String,colorCode:String,sampleFlag:Boolean,index:int,value:Number,inventoryDate:Date):void{
			_service.addEventListener(ResultEvent.RESULT, processupdateDetail);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.updateDetailMeterNum(name,colorCode,sampleFlag,index,value,inventoryDate);
		}
		
		private function processupdateDetail(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processupdateDetail);
			Env.dispatchEvent(new CustomerEvent("updateDetailSuccess",e.result as Boolean));
		}
		
		
		public function removeDust(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT, processRemoveDust);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.removeDust(id);
		}
		
		private function processRemoveDust(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,processRemoveDust);
			Env.dispatchEvent(new CustomerEvent("removeDustSuccess",e.result));
		}
		
	}
}